﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using quanlycuahangtruyen.ketnoi;



namespace quanlycuahangtruyen
{
    public partial class quanlykhachhang : Form
    {

        SqlConnection objConnect = new SqlConnection(Dbconnection.getconnectString());

        DataSet objDataset;
        DataView objDataview;

        SqlDataAdapter objDataApdater;
        CurrencyManager current;
        SqlCommand objCommand;
        private void diendl()
        {
            objDataset = new DataSet();

            objDataApdater = new SqlDataAdapter(" select *from TTKhachHang ", objConnect);
            objDataApdater.Fill(objDataset, "TTKhachHang");
            objDataview = new DataView(objDataset.Tables["TTKhachHang"]);




            current = (CurrencyManager)BindingContext[objDataview];
        }
        public quanlykhachhang()
        {
            InitializeComponent();
        }

        private  void  hienthi(){

            textmakhachhang.DataBindings.Clear();
            texthoten.DataBindings.Clear();
            textCMT.DataBindings.Clear();
            textdiachi.DataBindings.Clear();
            textsodienthoai.DataBindings.Clear();
            textmakhachhang.DataBindings.Add("text" ,objDataview,"makh");
            texthoten.DataBindings.Add("text", objDataview, "tenkh");
            textCMT.DataBindings.Add("text", objDataview, "cmnd");
            textdiachi.DataBindings.Add("text", objDataview, "diachi");
            textsodienthoai.DataBindings.Add("text", objDataview, "dienthoai");
            this.dataGridView1.DataSource = objDataview;
            

        }
        private void Current()
        {
            textdem.Text = (current.Position + 1).ToString();
        }
       
        private void quanlykhachhang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'quanLyChoThueTruyenDataSet.TTKhachHang' table. You can move, or remove it, as needed.
            this.tTKhachHangTableAdapter.Fill(this.quanLyChoThueTruyenDataSet.TTKhachHang);
            diendl();
            hienthi();

        }

        private void btback_Click(object sender, EventArgs e)
        {
            {
                current.Position -= 1;
                Current();
            }
        }

        private void btnext_Click(object sender, EventArgs e)
        {
            {
                current.Position += 1;
                Current();
            }

        }

        private void btnextall_Click(object sender, EventArgs e)
        {
            current.Position -= 1;

            current.Position = current.Count - 1;
            Current();

        }

        private void btbackall_Click(object sender, EventArgs e)
        {
            {
                current.Position = 0;
                Current();
            }
        }

        private void bt_Thêm_Click(object sender, EventArgs e)
        {
            if (bt_Thêm.Text == "Thêm")
            {
                textmakhachhang.Text = "";
                texthoten.Text = "";
                textCMT.Text = "";
                textdiachi.Text = "";
                textsodienthoai.Text = "";
                bt_Thêm.Text = "  &lưu";
            }
            else if (bt_Thêm.Text == "Lưu")
            {
                bt_Thêm.Text = "&Thêm";
                objCommand= new SqlCommand (" insert into  TTKhachHang( makh , Tenkh ,cmnd,diachi ,dienthoai) value( @makh, @tenkh, @cmtnd , @dienthoai)",objConnect);
                objCommand.Parameters.AddWithValue("@makh", textmakhachhang.Text);
                objCommand.Parameters.AddWithValue("@tenkh", texthoten.Text);
                objCommand.Parameters.AddWithValue("@cmnd", textCMT.Text);
                objCommand.Parameters.AddWithValue("@diachi", textdiachi.Text);
                objCommand.Parameters.AddWithValue("@dienthoai", textsodienthoai.Text);
                objConnect.Open();
                objCommand.ExecuteNonQuery();
                objConnect.Close();
                DataRowView newrow = objDataview.AddNew();
                newrow.BeginEdit();
                newrow["makh"] = textmakhachhang.Text;
                newrow["tenkh"] = texthoten.Text;
                newrow["cmnd"] = textCMT.Text;
                newrow["diachi"] = textdiachi.Text;
                newrow["dienthoai"] = textsodienthoai.Text;
                newrow.EndEdit();




            }
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            objCommand = new SqlCommand("update  TTKhachHang  set makh= @makh, tenKh= @Tenkh , cmnd =@cmnd , diachi=@diachi , dienthoai= @dienthoai",objConnect);
            objCommand.Parameters.AddWithValue("@makh", textmakhachhang.Text);
            objCommand.Parameters.AddWithValue("@tenkh", texthoten.Text);
            objCommand.Parameters.AddWithValue("@cmnd", textCMT.Text);
            objCommand.Parameters.AddWithValue("@diachi", textdiachi.Text);
            objCommand.Parameters.AddWithValue("@dienthoai", textsodienthoai.Text);
            objConnect.Open();
            objCommand.ExecuteNonQuery();
            objConnect.Close();
            DataRowView edit_Row = objDataview[Convert.ToInt32(textdem.Text) - 1];
            edit_Row.BeginEdit();
            edit_Row["@makh"] = textmakhachhang.Text;
            edit_Row["@tenkh"] = texthoten.Text;
            edit_Row["@cmnd"] = textCMT.Text;
            edit_Row["@diachi"] = textdiachi.Text;
            edit_Row["@dienthoai"] = textsodienthoai.Text;
            edit_Row.EndEdit();

        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn chắc chắn xóa bản ghi này?", "Thông báo",
               MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
               MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            objCommand = new SqlCommand("Delete from TTKhachHang  Where Makh=@Makh", objConnect);
            objCommand.Parameters.AddWithValue("@Mankh", textmakhachhang.Text);


            objConnect.Open();

            objCommand.ExecuteNonQuery();

            objConnect.Close();

            DataRowView delete_Row = objDataview[Convert.ToInt32(textdem.Text) - 1];
            delete_Row.Delete();
        }
    }
}
